package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.SoulOptions;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestionnaire;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.SoulApi;
import com.tanhua.dubbo.mapper.SoulOptionsMapper;
import com.tanhua.dubbo.mapper.SoulQuestionMapper;
import com.tanhua.dubbo.mapper.SoulQuestionnaireMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SoulApiImpl implements SoulApi {
    @Autowired
    private SoulQuestionnaireMapper soulQuestionnaireMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private SoulOptionsMapper soulOptionsMapper;

    /**
     * 查询试卷列表
     * @return
     */
    @Override
    public List<SoulQuestionnaire> queryQuestionnaire() {
        QueryWrapper<SoulQuestionnaire> questionnaireQueryWrapper = new QueryWrapper<>();
        return soulQuestionnaireMapper.selectList(questionnaireQueryWrapper);
    }

    /**
     * 查询题目列表，分页
     * @param page
     * @param pagesize
     * @param id
     * @return
     */
    @Override
    public PageResult queryQuestion(Integer page, Integer pagesize, String id) {
        QueryWrapper<SoulQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SoulQuestion::getQuestionnaireId, id);
        Page<SoulQuestion> page1 = new Page<>(page, pagesize);
        Page<SoulQuestion> soulQuestionPage = soulQuestionMapper.selectPage(page1, queryWrapper);
        return new PageResult(page,pagesize, (int) soulQuestionPage.getTotal(),soulQuestionPage.getRecords());
    }

    /**
     * 查询所有选项
     * @param page
     * @param pagesize
     * @param id
     * @return
     */
    @Override
    public PageResult queryOptions(Integer page, Integer pagesize, String id) {
        QueryWrapper<SoulOptions> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SoulOptions::getQuestionId, id);
        Page<SoulOptions> page1 = new Page<>(page, pagesize);
        Page<SoulOptions> soulOptionsPage = soulOptionsMapper.selectPage(page1, queryWrapper);
        return new PageResult(page,pagesize, (int) soulOptionsPage.getTotal(),soulOptionsPage.getRecords());
    }

    /**
     * 保存题目
     * @param questions
     */
    @Override
    public void saveQuestion(SoulQuestion questions) {
        soulQuestionMapper.insert(questions);
    }

    /**
     * 保存选项
     * @param options
     */
    @Override
    public void saveOption(SoulOptions options) {
        soulOptionsMapper.insert(options);
    }

    /**
     * 删除问题
     * @param id
     */
    @Override
    public void deleteQuestionById(Integer id) {
        soulQuestionMapper.deleteById(id);
    }

    /**
     * 删除问题对应的所有选项
     * @param id
     */
    @Override
    public void deleteOptionsByQuestionId(Integer id) {
        QueryWrapper<SoulOptions> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SoulOptions::getQuestionId, id);
        soulOptionsMapper.delete(queryWrapper);
    }

    /**
     * 删除单个选项
     * @param id
     */
    @Override
    public void deleteOptionById(Integer id) {
        soulOptionsMapper.deleteById(id);
    }

    /**
     * 查询单个问题
     * @param id
     * @return
     */
    @Override
    public SoulQuestion queryQuestionById(Integer id) {
        SoulQuestion question = soulQuestionMapper.selectById(id);
        return question;
    }

    /**
     * 修改问题
     * @param questions
     */
    @Override
    public void updateQuestion(SoulQuestion questions) {
        soulQuestionMapper.updateById(questions);
    }

    /**
     * 查询单个选项
     * @param id
     * @return
     */
    @Override
    public SoulOptions queryOptionById(Integer id) {
        SoulOptions option = soulOptionsMapper.selectById(id);
        return option;
    }

    /**
     * 修改选项
     * @param option
     */
    @Override
    public void updateOption(SoulOptions option) {
        soulOptionsMapper.updateById(option);
    }
}
